package com.example.tts.activity;

import android.os.Handler;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.util.DisplayMetrics;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



import com.blankj.utilcode.util.LogUtils;
import com.hospital.tts.R;
import com.hospital.tts.base.BaseActivity;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.Locale;


public class MainActivity extends BaseActivity implements View.OnClickListener, TextToSpeech.OnInitListener {
    private static final String TAG = "MainActivity";
    String resultString="";
    private EditText et_input;
    private Button bt_start;
    private TextView width,height;
    private TextToSpeech textToSpeech;

    //定义获取手机信息的SoapAction与命名空间,作为常量
    private static final String AddressnameSpace = "http://WebXml.com.cn/";
    //天气查询相关参数
    private static final String Weatherurl = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";
    private static final String Weathermethod = "getWeather";
    private static final String WeathersoapAction = "http://WebXml.com.cn/getWeather";
    //归属地查询相关参数
    private static final String Addressurl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
    private static final String Addressmethod = "getMobileCodeInfo";
    private static final String AddresssoapAction = "http://WebXml.com.cn/getMobileCodeInfo";

    private EditText edit_param;
    private Button btn_attribution;
    private Button btn_weather;
    private TextView txt_result;

    private String city;
    private String number;
    private String result;


    //定义一个Handler用来更新页面：
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x001:
                    txt_result.setText("结果显示：\n" + result);
                    Toast.makeText(MainActivity.this, "获取天气信息成功", Toast.LENGTH_SHORT).show();
                    break;
                case 0x002:
                    txt_result.setText("结果显示：\n" + result);
                    Toast.makeText(MainActivity.this, "号码归属地查询成功", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (textToSpeech==null){
            textToSpeech = new TextToSpeech(this, this);
        }
        LogUtils.e("启动主页面");
        initView();
        DisplayMetrics dm = getResources().getDisplayMetrics();

        int screenWidth = dm.widthPixels;

        int screenHeight = dm.heightPixels;
//        width = findViewById(R.id.width);
//        width.setText("width:"+screenWidth);
//
//        height = findViewById(R.id.height);
//        height.setText("height:"+screenHeight);

        final Handler handler=new Handler();
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                //要做的事情
//                onClick(null);
                handler.postDelayed(this, 10000);//表示第一次运行时的时间延迟
            }
        };
        handler.postDelayed(runnable, 2000);//每两秒执行一次runnable.
    }


    private void ttsParam() {
        textToSpeech.setPitch(1f);// 设置音调，,1.0是常规
        textToSpeech.setSpeechRate(0.9f);//设定语速，1.0正常语速
    }


    private void initView() {
//        et_input =(EditText) findViewById(R.id.et_input);
//        bt_start = (Button) findViewById(R.id.bt_start);

//        bt_start.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                textToSpeech.speak("中文语音测试", TextToSpeech.QUEUE_FLUSH, null);
//
//            }
//        });


//        edit_param=(EditText)findViewById(R.id.edit_param);
//        btn_attribution=(Button)findViewById(R.id.btn_attribution);
//        btn_weather=(Button)findViewById(R.id.btn_weather);
//        txt_result=(TextView)findViewById(R.id.txt_result);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.bt_start:
//
////                textToSpeech.speak("中文语音测试", TextToSpeech.QUEUE_FLUSH, null);
//
//                break;
//            case R.id.btn_weather:
//                new Thread() {
//                    @Override
//                    public void run() {
//                        getWether();
//                    }
//                }.start();
//                break;
//            case R.id.btn_attribution:
//                new Thread(new Runnable() {
//                    public void run() {
//                        getland();
//                    }
//                }).start();
//                break;
        }

    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            //初始化tts引擎
            int result = textToSpeech.setLanguage(Locale.CHINA);
            //设置参数
            ttsParam();
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "语音包丢失或语音不支持", Toast.LENGTH_SHORT).show();

            }
        }else {
            LogUtils.e("启动主页面:"+status);
        }
    }

    @Override
    protected void onDestroy() {
        if (textToSpeech!=null){
            //释放资源
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }



    //定义一个获取某城市天气信息的方法：
    public void getWether() {
        result = "";
        SoapObject soapObject = new SoapObject(AddressnameSpace, Weathermethod);
        soapObject.addProperty("theCityCode:", edit_param.getText().toString());
        soapObject.addProperty("theUserID", "dbdf1580476240458784992289892b87");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTransportSE = new HttpTransportSE(Weatherurl);
        System.out.println("天气服务设置完毕,准备开启服务");
        try {
            httpTransportSE.call(WeathersoapAction, envelope);
//            System.out.println("调用WebService服务成功");
        } catch (Exception e) {
            e.printStackTrace();
//            System.out.println("调用WebService服务失败");
        }

        //获得服务返回的数据,并且开始解析
        SoapObject object = (SoapObject) envelope.bodyIn;
        System.out.println("获得服务数据");
        result = object.getProperty(1).toString();
        handler.sendEmptyMessage(0x001);
        System.out.println("发送完毕,textview显示天气信息");
    }


    //定义一个获取号码归属地的方法：
    public void getland() {
        result = "";
        SoapObject soapObject = new SoapObject(AddressnameSpace, Addressmethod);
        soapObject.addProperty("mobileCode", edit_param.getText().toString());
        soapObject.addProperty("userid", "dabf121232143254854999dbfg");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTransportSE = new HttpTransportSE(Addressurl);
        try {
            httpTransportSE.call(AddresssoapAction, envelope);
            //System.out.println("调用WebService服务成功");
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.println("调用WebService服务失败");
        }

        //获得服务返回的数据,并且开始解析
        SoapObject object = (SoapObject) envelope.bodyIn;//System.out.println("获得服务数据");
        result = object.getProperty(0).toString();//System.out.println("获取信息完毕,向主线程发信息");
        handler.sendEmptyMessage(0x002);

    }


}
